package cn.sky.demo;

import cn.sky.demo.domain.vo.Node;

import java.sql.*;
import java.util.*;

/**
 * @author sky
 * @date 2025年09月19日 14:15
 * @description
 * @since 1.0.0
 */
public class MyDataTree{
    private List<Node> dataArray=new ArrayList<Node>();
    private Map<Integer, Node> tree=new HashMap<Integer, Node>();
    public void initArray(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3356/mybatis_plus?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";
            Connection conn = DriverManager.getConnection(url, "root", "123456");
            String sql ="select * from `node`";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Node node = new Node();
                node.setId(rs.getInt("id"));
                node.setPid(rs.getInt("pid"));
                node.setTitle(rs.getString("title"));
                dataArray.add(node);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public void printArray(){
        if(dataArray.size()>0){
            /*for(Iterator<Node> it = dataArray.iterator(); it.hasNext();){
                Node node=it.next();
                System.out.println(node.getId()+"--"+node.getPid()+"--"+node.getTitle());
            }*/
            for(Node node:dataArray){
                System.out.println(node.getId()+"--"+node.getPid()+"--"+node.getTitle());
            }
        }
    }

    public void buildTree(){
        for(Node node:dataArray){
            if(node.getPid()==0){
                tree.put(node.getId(), node);
            }else{
                Node parentNode = tree.get(node.getPid());
                parentNode.getChildrens().add(node);
                tree.put(node.getId(), node);
            }
        }
    }

    public void printTree(){
        if(tree.size()>0){
            for(Node node:tree.values()){
                if(node.getPid()==0) {
                    System.out.println("---节点编号：-"+node.getId() + "-父节点：-" + node.getPid() + "-内容-" + node.getTitle());
                    getAllChildren(node,1);
                }
            }
        }
    }

    private void getAllChildren(Node node, int level) {
        String str="----";
        for (int i = 0; i < level; i++){
            str+="----";
        }
        if(node.getChildrens().size()>0){
            for(Node child:node.getChildrens()){
                System.out.println(str+"---节点编号：-"+child.getId() + "-父节点：-" + child.getPid() + "-内容-" + child.getTitle());
                getAllChildren(child,level+1);
            }
        }
    }

    public static void main(String[] args) {
       MyDataTree myDataTree=new MyDataTree();
       myDataTree.initArray();
      // myDataTree.printArray();
       myDataTree.buildTree();
       myDataTree.printTree();
    }
}
